<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/6/23
  Time: 13:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户注册</title>
</head>
<body>

<%
            String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
            String password = request.getParameter("password");

                Connection connection= null;
                Statement statement= null;
                ResultSet resultSet =null;



            Class.forName("com.mysql.jdbc.Driver");
            String url;
            connection = DriverManager.getConnection(
            url= "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
            "root",
            "123456"//登录JDBC链接数据库
    );

             statement = connection.createStatement();

            Connection conn = DriverManager.getConnection(url,username,password);
            PreparedStatement pStmt = conn.prepareStatement("select * from tt where username='"+username+"' and password='"+ password + "'");
              ResultSet rs = pStmt.executeQuery();
                if(rs.next()){
                    out.println("<script language='javascript'>alert('该用户已存在，请重新注册！');window.location.href='register.jsp';</script>");
                }else{
                    PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + username + "','" + password + "')");
                        int rst = tmt.executeUpdate();
                        if (rst != 0){
                              out.println("<script language='javascript'>alert('用户注册成功！');window.location.href='index.jsp';</script>");
                        }else{
                           out.println("<script language='javascript'>alert('用户注册失败！');window.location.href='register.jsp';</script>");
                        }
                }

                statement.close();
                connection.close();
     %>
</body>
</html>
